define(['jquery', 'bootstrap', 'frontend', 'form', 'template','echarts'], function ($, undefined, Frontend, Form, Template, Echarts) {
    var validatoroptions = {
        invalid: function (form, errors) {
            $.each(errors, function (i, j) {
                Layer.msg(j);
            });
        }
    };
    var Controller = {
        reading:function(){
            $(document).on('click', "input[type='checkbox']", function () {
                if ($(this).attr("checked")) {
                   $(".btn-reading").addClass('disabled')
                   $(".btn-reading").addClass('btn-disabled')
                   $(".btn-reading").attr('href','javascript:;')
                   $(this).removeAttr("checked");
                } else { 
                   $(".btn-reading").removeClass('disabled')
                   $(".btn-reading").removeClass('btn-disabled')
                   $(".btn-reading").attr('href','/index/suitable/vocation/code/'+Config.code)
                   $(this).attr("checked", "true");
                }
            });

            $(document).ready(function() {
                var targetDate = Config.sg_end; // 目标日期时间戳
                var now = Config.sg_start; // 当前时间时间戳
                var distance = targetDate - now;
             
                var days = Math.floor(distance / (1000 * 60 * 60 * 24));
                var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                var seconds = Math.floor((distance % (1000 * 60)) / 1000);
             
                // 更新倒计时
                var timer = setInterval(function() {
                    now = new Date().getTime();
                    // console.log(now);
                    // console.log(Config.sg_start);
                    distance = targetDate - now;
                    days = Math.floor(distance / (1000 * 60 * 60 * 24));
                    hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
                    minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
                    seconds = Math.floor((distance % (1000 * 60)) / 1000);
             
                    // 输出结果到页面
                    $('#countdown').html(days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒");
             
                    // 如果倒计时结束，清除计时器
                    if (distance < 0) {
                        clearInterval(timer);
                        $('#countdown').html("时间到！");
                    }
                }, 1000);
            });
        },
        index:function () {
            // $(document).on('click','.btn-describe',function () {
            //     var tishi = '<div class="mui-popup-text mui-content" style="text-align: left;">';
            //     tishi +='<span style="text-align:left;">'+Config.sg_ceping_xuzhi+'</span><br/>';    
            //     tishi +='</div>';
            //     Layer.alert(tishi);
            //     $(".layui-layer-title").html('测评须知');
            // });
            localStorage.setItem('suitable-answers','');
            localStorage.setItem('new-answer_keys','');
            localStorage.setItem('resumption-answers','')

            Layer.msg('清理缓存成功');
        },
        vocation: function () {
            var answer_keys = [];
            var answer_arr = Config.pagearr;

            // localStorage.setItem('suitable-answers','');
            // localStorage.setItem('new-answer_keys','');

            var count = Config.count;
            var select_count = 0;
            var a2= localStorage.getItem('suitable-answers');
            
            if(a2){
                answers = JSON.parse(a2);
                if(Config.type>0){
                    $('.subject').hide();
                    $('.subject_'+Config.type).show();
                    $('.subject_'+Config.type +' .answer').removeClass('btninfo1');

                    $.each(answers[Config.type],function (d, v) {
                        $('.subject_'+v.id +' .answer').each(function(dd,vv) {
                            var vals = $(this).data('val');
                            var ids = $(this).data('id');
                            var types = $(this).data('type');
                            if(ids==v.id && vals == v.answer && types == v.type){
                                $(this).addClass('btninfo1');
                            }
                        });
                    });
                    //最后一页文案改成提交
                    if(count == Config.type){
                        $(".btn-next").css('display','none');
                        $(".btn-submit").show();
                    }
                }else{
                    console.log(answers);
                    var item_id = count;
                    $.each(answers,function (index, item) {
                        if(answers[index].length==0){
                            item_id = index;
                            return false;
                        }else{
                            // $.each(answers[index],function (ins, val) {
                            //     $('.subject_'+val.id +' .answer').each(function(dd,vv) {
                            //         var valed = $(this).data('val');
                            //         var idsed = $(this).data('id');
                            //         var typesed = $(this).data('type');
                            //         if(idsed==val.id && valed == val.answer && typesed == val.type){
                            //             $(this).addClass('btninfo1');
                            //         }
                            //     });
                            // });
                        }
                    });
                    console.log(item_id);
                    $('.subject').hide();
                    $('.subject_'+item_id).show();
                    $('.subject_'+item_id +' .answer').removeClass('btninfo1');
                    if(count == item_id){
                        $(".btn-next").css('display',"none");
                        $(".btn-submit").show();
                    }

                    $.each(answers,function (index, item) {
                        $.each(answers[index],function (ins, val) {
                            $('.subject_'+val.id +' .answer').each(function(dd,vv) {
                                var valed = $(this).data('val');
                                var idsed = $(this).data('id');
                                var typesed = $(this).data('type');
                                if(idsed==val.id && valed == val.answer && typesed == val.type){
                                    $(this).addClass('btninfo1');
                                }
                            });
                        });
                    });

                }
                var newanswerkeys = localStorage.getItem('new-answer_keys');
                $.each( JSON.parse(newanswerkeys),function (dd, vv) {
                    if(answer_keys.indexOf(vv) == -1){
                        answer_keys.push(vv)
                    }
                })
                answer_arr = answers;
            }
            
            $(document).on('click','.answer',function () {
                var type = $(this).data('type');
                var answer = $(this).data('val');
                var id = $(this).data('id');

                select_count = select_count+1;
                //选中颜色
                if(type=='zonghesuzhi'){
                    $('.answer-zonghesuzhi').each(function(index) {
                        var an =  $(this).data('val');
                        var ids = $(this).data('id');
                        if(id==ids && an == answer){
                            $(this).addClass('btninfo1');
                        }
                        if(id==ids && an != answer){
                            $(this).removeClass('btninfo1');
                        }
                    });
                }else if(type=='yewunengli'){
                    $('.answer-yewunengli').each(function(index) {
                        var an =  $(this).data('val');
                        var ids = $(this).data('id');
                        if(id==ids &&an == answer){
                            $(this).addClass('btninfo1');
                        }
                        if(id==ids && an != answer){
                            $(this).removeClass('btninfo1');
                        }
                    });
                }else if(type=='lvzhijinze'){
                    $('.answer-lvzhijinze').each(function(index) {
                        var an =  $(this).data('val');
                        var ids = $(this).data('id');
                        if(id==ids && an == answer){
                            $(this).addClass('btninfo1');
                        }
                        if(id==ids && an != answer){
                            $(this).removeClass('btninfo1');
                        }
                    });
                }else if(type=='banzijianshe'){
                    $('.answer-banzijianshe').each(function(index) {
                        var an =  $(this).data('val');
                        var ids = $(this).data('id');
                        if(id==ids && an == answer){
                            $(this).addClass('btninfo1');
                        }
                        if(id==ids && an != answer){
                            $(this).removeClass('btninfo1');
                        }
                    });
                }else if(type=='shengtaifenwei'){
                    $('.answer-shengtaifenwei').each(function(index) {
                        var an =  $(this).data('val');
                        var ids = $(this).data('id');
                        if(id==ids && an == answer){
                            $(this).addClass('btninfo1');
                        }
                        if(id==ids && an != answer){
                            $(this).removeClass('btninfo1');
                        }
                    });
                }else if(type=='gongzuoshiji'){
                    $('.answer-gongzuoshiji').each(function(index) {
                        var an =  $(this).data('val');
                        var ids = $(this).data('id');
                        if(id==ids && an == answer){
                            $(this).addClass('btninfo1');
                        }
                        if(id==ids && an != answer){
                            $(this).removeClass('btninfo1');
                        }
                    });
                }
                if(answer_keys.indexOf(id) == -1){
                    answer_keys.push(id)
                }
            })


            //点击下一页
            $(document).on('click','.btn-next',function () {
                var that = $(this);
                var id = that.data('id');
                var idss = id+1;
                var isset_input_count = 0;
                //判断完成了几项
                $('.subject_'+id+' .answer').each(function(index) {
                    var value = $(this).val();
                    if($(this).hasClass('btninfo1')){
                        isset_input_count = isset_input_count+1;
                    }
                });
                if(isset_input_count!=6){
                    Layer.msg('请先完成六项测评');
                    return false
                }
                //end

                var a = [];
                //将当前页的选项加入缓存 
                $('.subject_'+id+' .answer').each(function(index) {
                    if($(this).hasClass('btninfo1')){
                        var answer = $(this).data('val');
                        var type = $(this).data('type');
                        var idd = $(this).data('id');
                        a.push({id:idd,type:type,answer:answer});
                    }
                    if(a.length>0) {
                        answer_arr[id] = a;
                        localStorage.setItem('suitable-answers',JSON.stringify(answer_arr));
                    }
                });
                localStorage.setItem('new-answer_keys',JSON.stringify(answer_keys));
                //end
                
                console.log(answer_arr);
                console.log('下一页answer_arr');

                //完成六项后可跳转到下一页
                if(isset_input_count == 6 ){
                    $('.subject_'+id).hide();

                    //展示下一页
                    $('.subject_'+idss).show();
                    $('.subject_'+idss +' .answer').removeClass('btninfo1');
                    if(count==idss){
                        $(".btn-next").css('display','none');
                        $(".btn-submit").show();
                    }
                    //如果有缓存，下一页显示
                    var suitable_answers= localStorage.getItem('suitable-answers');
                    if(suitable_answers && count!=id){
                        answerss = JSON.parse(suitable_answers);
                        $.each(answerss[idss],function (d, vs) {
                            $('.subject_'+idss +' .answer').each(function(dd,vv) {
                                var vals = $(this).data('val');
                                var ids = $(this).data('id');
                                var types = $(this).data('type');
                                if(ids==vs.id && vals == vs.answer && types == vs.type){
                                    $(this).addClass('btninfo1');
                                }
                            });
                        });
                    }
                }

            });


            //点击上一页
            $(document).on('click','.btn-last',function () {
                var id = $(this).data('id');
                if(id==1){
                    Layer.msg('当前已经是第一个');
                    return false
                }
                $('.subject_'+id).hide();
                $('.subject_'+(id-1)).show();
                $(".btn-next").show();
                $(".btn-submit").css('display','none');

                var idss = id-1;
                //读取上一页的缓存数据
                var last_answers= localStorage.getItem('suitable-answers');
                if(last_answers){
                    answerss = JSON.parse(last_answers);
                    $.each(answerss[idss],function (d, vs) {
                        $('.subject_'+idss +' .answer').each(function(dd,vv) {
                            // $(this).removeClass('btninfo1');
                            var vals = $(this).data('val');
                            var ids = $(this).data('id');
                            var types = $(this).data('type');
                            if(ids==vs.id && vals == vs.answer && types == vs.type){
                                $(this).addClass('btninfo1');
                            }
                        });
                    });
                }

                var b = [];
                //将当前页的数据加入缓存
                $('.subject_'+id+' .answer').each(function(index) {
                    if($(this).hasClass('btninfo1')){
                        var answer = $(this).data('val');
                        var type = $(this).data('type');
                        var idd = $(this).data('id');
                        b.push({id:idd,type:type,answer:answer});
                    }
                    if(b.length>0) {
                        answer_arr[id] = b;
                        localStorage.setItem('suitable-answers',JSON.stringify(answer_arr));
                    }
                });
                localStorage.setItem('new-answer_keys',JSON.stringify(answer_keys));
                console.log(answer_arr);
                console.log('上一页answer_arr');
            });

            //提交
            $(document).on('click','.btn-submit',function () {
                var that = $(this);
                var id = that.data('id');
                var isset_input_count = 0;
                $('.subject_'+id+' .answer').each(function(index) {
                    var value = $(this).val();
                    if($(this).hasClass('btninfo1')){
                        isset_input_count = isset_input_count+1;
                    }
                });
                if(isset_input_count!=6){
                    Layer.msg('请先完成六项测评');
                    return false
                }
                var mycount= answer_keys.length;
                console.log(count+'@@@'+mycount);

                //还需要把当前最后一页的数据加到 缓存当中
                if(count != mycount){
                    Layer.msg('请确保对全部人员已打分');
                    return false
                }

                var a = [];
                $('.subject_'+id+' .answer').each(function(index) {
                    if($(this).hasClass('btninfo1')){
                        var answer = $(this).data('val');
                        var type = $(this).data('type');
                        var idd = $(this).data('id');
                        a.push({id:idd,type:type,answer:answer});
                    }
                    if(a.length>0) {
                        answer_arr[id] = a;
                        // localStorage.setItem('suitable-answers',JSON.stringify(answer_arr));
                    }
                });

            
                if(count == mycount && isset_input_count == 6 ){
                    var layerIndex = layer.load(3, {
                        shade: [0.7,'gray'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "/index/suitable/vocation/code/"+Config.code,
                        type: 'post',
                        dataType: 'json',
                        data: {answer_arr:answer_arr},
                        success: function (ret) {
                            if(ret.code == 0){
                                layer.alert('请刷新重试');
                                layer.close(layerIndex);
                            }else{
                                localStorage.setItem('suitable-answers','');
                                localStorage.setItem('new-answer_keys','');

                                layer.alert('感谢您的打分，测评全部完成！', {
                                    btn: ['确定', '取消'], // 按钮
                                    yes: function(index){
                                        // 点击确定按钮的回调
                                        window.location.href = "/index/suitable/reading/code/"+Config.code; //跳转到指定URL
                                        layer.close(index); // 关闭弹窗
                                    }
                                }, function(index){
                                    // 点击取消按钮的回调
                                    window.location.href = "/index/suitable/reading/code/"+Config.code; //跳转到指定URL
                                    layer.close(index); // 关闭弹窗
                                });
                            }
                        }
                    });
                }
                console.log(count +'=='+ mycount);
            });


            
            //弹出测评表
            $(document).on('click','.jieshao',function () {
                var tishi = '<div class="mui-popup-text mui-content" style="text-align: left;">';
                tishi +='<span style="text-align:left;">'+Config.sg_ceping_neirong+'</span><br/>';
                tishi +='</div>';
                Layer.alert(tishi);
                $(".layui-layer-title").html('');
            })

            //人员列表
            $(document).on('click','.btn-rylb',function () {
                localStorage.setItem('new-answer_keys',JSON.stringify(answer_keys));
                window.location.href = "/index/suitable/person/code/"+Config.code;
            });
            
        },
        person:function(){
            var aperson= localStorage.getItem('suitable-answers');
            if(aperson){ 
                answerslog = JSON.parse(aperson);
                var tishi = '';
                for (var i = 0; i < Config.subjects.length; i++) {
                    ret = Config.subjects[i];
                    tishi +='   <tr>';
                    tishi += '    <td>'+ret.name+'</td>';
                    if(ret.department){
                        tishi += '    <td>'+ret.department+'</td>';
                    }else{
                        tishi += '    <td></td>';
                    }
                    //未作选择
                    if(answerslog.length==0){
                        tishi += '    <td>未打分</td>';
                        tishi += '    <td><a class="btn btn-xs btn-success tm" data-id="'+ret.id+'">打分</a></td>';
                    }else{
                        var zhuijia ='    <td>未打分</td><td><a class="btn btn-xs btn-success tm" data-id="'+ret.id+'">打分</a></td>';
                        $.each(answerslog,function (d, v) {
                            $.each(v,function (d11, v11) {
                                if(v11.id==ret.id){
                                    zhuijia = '    <td>已打分</td><td><a class="btn btn-xs btn-info tm" data-id="'+ret.id+'">重打</a></td>';
                                    return false;
                                }
                            });
                        });
                        tishi += zhuijia;
                    }

                    tishi +='   </tr>';
                }
                $(".task_list").html(tishi);
            }

            //重打
            $(document).on('click','.tm',function () {
                var ids = $(this).data("id");
                window.location.href = "/index/suitable/vocation/code/"+Config.code+'/type/'+ids;
            });
        },
    };
    return Controller;
});